import logging

from aiogram import Bot, types
from aiogram_dialog import DialogManager
from fluentogram import TranslatorRunner

from poker_bot.config import Settings
from poker_bot.core.models.enums.account import AccountStatus
from poker_bot.database.services import AccountService, UserService
from poker_bot.tg_bot.services.locale import Localizator
from poker_bot.tg_bot.services.tg_helpers import send_tg_message

logger = logging.getLogger(__name__)


async def confirm_account(call: types.CallbackQuery, _, manager: DialogManager):
    """Подтверждаем аккаунт"""

    bot: Bot = manager.middleware_data.get("bot")
    localizator: Localizator = manager.middleware_data.get("localizator")
    locale: TranslatorRunner = manager.middleware_data.get("locale")
    config: Settings = manager.middleware_data.get("config")
    user_service: UserService = manager.middleware_data.get("user_service")
    account_service: AccountService = manager.middleware_data.get("account_service")

    account_id: int = manager.dialog_data.get("account_id")
    account = await account_service.get_account_by_id(account_id)
    account_owner = await user_service.get_user_by_id(account.user_id)

    await account_service.update_account_status(int(account_id), AccountStatus.approved)
    await user_service.change_selected_account(
        account_owner.telegram_id, int(account_id)
    )
    await send_tg_message(
        bot,
        config.tg_bot.admin_channel_id,
        locale.get(
            "account-confirmed-by-admin-msg",
            account_id=account_id,
            admin_tg_id=str(call.from_user.id),
        ),
    )

    # Отправляем сообщение пользователю
    user_locale: TranslatorRunner = localizator.get_by_locale(account_owner.lang_code)

    await send_tg_message(
        bot,
        account_owner.telegram_id,
        user_locale.get("your-account-confirmed-msg"),
    )

    await call.answer(locale.get("account-confirmed-msg"))
    await manager.done()
